package cn.kgc.mushroommanager.controller;

import cn.kgc.mushroomcommon.api.DataBaseOperationRemoteService;
import cn.kgc.mushroomcommon.pojo.Type;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/type")
public class TypeController {

    @Resource
    DataBaseOperationRemoteService dataBaseOperationRemoteService;

    @RequestMapping("/selectType")
    public List<Type> selectType(){
        try {
         List<Type> types = dataBaseOperationRemoteService.selectType();
        //把数据存放在map里
        Map<Integer,Type> map = new HashMap<>();
        for (Type type : types) {
            Type type1 = new Type(type.getId(),type.getTidfather(),type.getTypename());
            System.out.println(type1);
            map.put(type1.getId(),type1);
        }
            System.out.println(map);
        //循环map并添加子节点
        for (Type type02 : map.values()) {
             if(type02.getTidfather()!=null){
                 map.get(type02.getTidfather()).getChildrens().add(type02);
             }
        }
        //声明最终结果集
        List<Type> tagVos = new ArrayList<>();
        for (Type type03 : map.values()) {
            //判断是否是父节点
            if(type03.getTidfather()==null){
                tagVos.add(type03);
            }
        }
        return tagVos;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @RequestMapping("/selectshoptype")
    public List<Type> selectshoptype(@RequestParam("sid")Integer sid){
        List<Type> selectshoptype = dataBaseOperationRemoteService.selectshoptype(sid);
        return selectshoptype;
    }
}

